/*******************************************************************************
* File Name          : main.c
* Author             : ariff_ehsan
* Date First Issued  : Sept. 15, 2009
* Description        : Main source file.
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#include "dled.h"
#include "dlcd.h"
#include "dbutton.h"

#include "toolbar.h"


void RCC_Configuration(void);
void NVIC_Configuration(void);

static void startupTask( void *pvParameters );

RCC_ClocksTypeDef RCC_ClockFreq;
ErrorStatus HSEStartUpStatus;


int main( void )
    {
    RCC_Configuration();
    NVIC_Configuration();
    
    /* Configure HCLK clock as SysTick clock source. */
	SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
    
    /* create startup task */
    xTaskCreate( startupTask, ( signed portCHAR * ) "Startup",
                 configMINIMAL_STACK_SIZE, NULL, startup_TASK_PRIORITY, NULL );
    
    /* Start the scheduler. */
	vTaskStartScheduler();
    
    return 0;
    }

void RCC_Configuration( void ){
    
    /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/   
    /* RCC system reset(for debug purpose) */
    RCC_DeInit();

    /* Enable HSE */
    RCC_HSEConfig(RCC_HSE_ON);

    /* Wait till HSE is ready */
    HSEStartUpStatus = RCC_WaitForHSEStartUp();

    if (HSEStartUpStatus == SUCCESS)
    {
        /* Enable Prefetch Buffer */
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        /* Flash 2 wait state */
        FLASH_SetLatency(FLASH_Latency_2);
     
        /* HCLK = SYSCLK */
        RCC_HCLKConfig(RCC_SYSCLK_Div1); 
      
        /* PCLK2 = HCLK */
        RCC_PCLK2Config(RCC_HCLK_Div1); 

        /* PCLK1 = HCLK/2 */
        RCC_PCLK1Config(RCC_HCLK_Div2);

        /* PLLCLK = 12MHz * 6 = 72 MHz */
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);

        /* Enable PLL */ 
        RCC_PLLCmd(ENABLE);

        /* Wait till PLL is ready */
        while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
        {
        }

        /* Select PLL as system clock source */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        /* Wait till PLL is used as system clock source */
        while(RCC_GetSYSCLKSource() != 0x08)
        {
        }
    }
    else
    { /* If HSE fails to start-up, the application will have wrong clock configuration.
         User can add here some code to deal with this error */    

        /* Go to infinite loop */
        while (1)
        {
        }
    }

    /* This function fills the RCC_ClockFreq structure with the current
       frequencies of different on chip clocks (for debug purpose) */
    RCC_GetClocksFreq(&RCC_ClockFreq);

    /* Enable Clock Security System(CSS): this will generate an NMI exception
       when HSE clock fails */
    RCC_ClockSecuritySystemCmd(ENABLE);
}

void NVIC_Configuration(void){

    /* Set the Vector Table base location at 0x08000000 */ 
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
    
    /* Configure two bits for preemption priority */
    NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
}

const u8 main_text_amrcortexm3[] = "arm cortex-m3";
const u8 main_text_freertos[] = "freeRTOS";
const u8 main_text_starting[] = "starting...";

static void startupTask( void *pvParameters )
    {
    const portTickType xDelay2000 = 2000 / portTICK_RATE_MS;
    
    /* start leds service */
    LED_Configuration();
    
    LED_SetMode( LED_GREEN, LED_ON );
    
    vTaskDelay(xDelay2000);
    
    /* start lcd service */
    LCD_Configuration();
    
    LCD_GetResource();
    
    LCD_PutString( 20, 20, main_text_amrcortexm3, sizeof( main_text_amrcortexm3 ) );
    vTaskDelay(xDelay2000);
    
    LCD_DrawFilledRect( 0, 0, 128, 128, RGB_WHITE );
    
    LCD_PutString( 20, 20, main_text_freertos, sizeof( main_text_freertos ) );
    vTaskDelay(xDelay2000);
    
    LCD_DrawFilledRect( 0, 0, 128, 128, RGB_WHITE );
    
    //LCD_DrawFilledRect( 54, 54, 10, 10, RGB_RED );
    
    LCD_ReleaseResource();
    
    LED_SetMode( LED_GREEN, LED_OFF );
    
    BUTTON_Configuration();
    
    TOOLBAR_Configuration();
    
    /* kill itself */
    vTaskDelete( NULL );
    
    while(1)
        {
        }
    }


#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif
